<template>
  <div class="cate-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div>
        <el-button type="primary" @click="cateAdd">添加分类</el-button>
        <!-- 表格 -->
        <zk-table
          :data="cateList"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          :show-index="true"
        >
          <template slot="cat_deleted" slot-scope="scoped">
            <i v-if="scoped.row.cat_deleted" class="el-icon-error"></i>
            <i style="color:green" v-else class="el-icon-success"></i>
          </template>
          <template slot="cat_level" slot-scope="scoped">
            <el-tag v-if="scoped.row.cat_level==0">一级</el-tag>
            <el-tag v-else-if="scoped.row.cat_level==1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
          <template slot="cat_id" slot-scope="scoped">{{scoped.row.cat_id}}</template>
        </zk-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="cateParams.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="cateParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>

        <!-- 增加弹出框 -->
        <el-dialog title="添加分类" :visible.sync="dialogFormVisibleAdd">
          <el-form ref="formDialogAddCate" :model="formAddCate" :rules="rulesFormAddCate">
            <el-form-item prop="cat_name" label="分类名称" label-width="120px">
              <el-input v-model="formAddCate.cat_name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="父级分类" label-width="120px">
              <!-- //级联选择器 -->
              <el-cascader
                v-model="value"
                :options="cateCasList"
                :props="props"
                clearable
                change-on-select
              ></el-cascader>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addForm">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>
           
<script>
import { cateList_api, cateAdd_api } from "@/api";
export default {
  data() {
    return {
      cateParams: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      cateList: [],
      total: 1,
      //表格标题数据
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "cat_deleted",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "cat_level",
        },
        {
          label: "操作",
          prop: "cat_id",
          type: "template",
          template: "cat_id",
        },
      ],
      dialogFormVisibleAdd: false,
      formAddCate: {
        cat_pid: "",
        cat_name: "",
        cat_level: "",
      },
      cateCasList: [],
      //  表单校验规则
      rulesFormAddCate: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      /* 级联续择器 */
      value: [],
      props: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await cateList_api(this.cateParams);
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    /* 分页 */
    handleSizeChange(val) {
      this.cateParams.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      this.cateParams.pagenum = val;
      this.getCateList();
    },
    async cateAdd() {
      this.dialogFormVisibleAdd = true;
      const { data: res } = await cateList_api({ type: 2 });
      this.cateCasList = res.data;
    },
    addForm() {
      // 表单校验
      this.$refs.formDialogAddCate.validate(async (valide) => {
        if (!valide) {
          return;
        } else {
          if (this.value.length > 0) {
            this.formAddCate.cat_pid = this.value[this.value.length - 1];
          } else {
            this.formAddCate.cat_pid = 0;
          }
        }
        this.formAddCate.cat_level = this.value.length;
        const { data: res } = await cateAdd_api(this.formAddCate);
        console.log(res, 999999);
        this.dialogFormVisibleAdd = false;
        this.getCateList();
        // 清空列表
        this.$refs.formDialogAddCate.resetFields();
        this.value = [];
      });
    },
  },
};
</script>
<style scoped lang='less'>
.cate-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
  .el-button {
    margin-bottom: 20px;
  }
}
</style>